
package com.rt.schedulenew.utils.coder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;

public abstract class SHACoder extends SecurityCoder
{
    public static byte[] encodeSHA(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("SHA");
        return md.digest(data);
    }
    
    public static byte[] encodeSHA1(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("SHA-1");
        return md.digest(data);
    }
    
    public static byte[] encodeSHA256(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("SHA-256");
        return md.digest(data);
    }
    
    public static byte[] encodeSHA384(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("SHA-384");
        return md.digest(data);
    }
    
    public static byte[] encodeSHA512(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("SHA-512");
        return md.digest(data);
    }
    
    public static byte[] encodeSHA224(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("SHA-224");
        return md.digest(data);
    }
    
    public static String HMACSHA256(final String data, final String key) throws Exception {
        final Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        final byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        final StringBuilder sb = new StringBuilder();
        for (final byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }
}
